import logging

import pytest

from resource.pom.gallery import Gallery
from resource.pom.keyboard import Keyboard
from utils.manager import MatchRules


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.video1, app.videos_dir)
    device.mediatool_send(app.videos_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击图库，选择一个视频点击进入')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        # 照片
        tab_photos = device.get_element_by_key('TabPhotos')
        # 相册
        tab_albums = device.get_element_by_key('TabAlbums')
        device.click_element(tab_photos)
        device.refresh_layout()
        gesture = device.get_element_by_key('Gesture_1')
        device.click_element(gesture)
        logging.info('预期结果1.进入到视频')
        device.refresh_layout()
        device.refresh_layout()
        device.assert_key_exist('PhotoBrowserBg')
        device.assert_key_exist('ActionBarButtonBack')
        device.assert_key_exist('PhotoBrowserDateTitle')
        device.assert_key_exist('DetailMenuPanel')
        device.assert_key_exist('ActionBarButtonInfo')
        device.assert_key_exist('ToolBarButtonFavor')
        device.assert_key_exist('ToolBarButtonEdit')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_key_exist('ToolBarButtonMore')

        logging.info('步骤2.点击更多-重命名，输入新的名称后点击确定')
        device.click_element(device.get_element_by_key('ToolBarButtonMore'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('重命名'))
        device.refresh_layout()
        rename_textinput = device.get_element_by_key('RenameDialogTextInput')
        current_name = rename_textinput.get('text')
        device.click_element(rename_textinput)
        logging.info('预期结果2.唤醒键盘，输入名称正常，确定按钮有效')
        device.refresh_layout()
        device.assert_soft_keyboard_on()
        # 输入一个a
        device.click(*Keyboard.key_a)
        device.click_element(device.get_element_by_text('确定'))

        logging.info('步骤3.退出视频后长按视频，点击更改-详细信息查看')
        device.go_back()
        device.long_click_element(gesture)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('ToolBarButtonMore'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('详细信息'))
        logging.info('预期结果3.在详细信息中可以查看到视频修改后的名称')
        device.refresh_layout()
        new_name = device.get_element_by_key('ValueOfDetailTitle').get('text')
        assert new_name == current_name + 'a', '重命名失败'
